/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : main.c                                         	      *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-12-20                                            *
 *  $AutoSAR Version  : V1.0	                                              *
 *  $Description$     : This example demonstrates how to wake up by external  * 
 * 						interrupt in normal sleep mode						  *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyrght protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-12-20 00.01.00 FAE Group     new creat                               *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
**                      		Include Files                                **
******************************************************************************/
#include "system_init.h"

/******************************************************************************
**                      	Private variables                                **
******************************************************************************/
static volatile uint32_t TimingDelay;
static volatile uint32_t SleepCount;

/*******************************************************************************
**                     		   Global Functions 		             	      **
*******************************************************************************/
/**
 *  @brief :Initialize the LED GPIO ports
 * 			PD4	------------- UserKey
 * 			PF11 ------------ LED1
 * 			PA3 ------------- LED2
 *  @param in :None
 *  @param out :None 
 *  @retval :None
 */
void BoardGpioInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	/* Configure PF11 LED1 output */
	GPIO_InitStructure.m_Mode = GPIO_MODE_OUT;
	GPIO_InitStructure.m_OpenDrain = GPIO_POD_PP;
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;
	GPIO_InitStructure.m_PullUp = GPIO_NOPULL;
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_11;
	GPIO_Configuration(GPIOF_SFR , &GPIO_InitStructure);
	
	/* Configure PA3 LED1 output */
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_3;
	GPIO_Configuration(GPIOA_SFR , &GPIO_InitStructure);

	/* Configure PD4 user key input */
	GPIO_InitStructure.m_Mode = GPIO_MODE_IN;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_4;
	GPIO_Configuration(GPIOD_SFR , &GPIO_InitStructure);
}

/**
 *  @brief :Configure the PD4 is interrupt mode, and working in rising and falling edges
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
void BoardUserKeyInit(void)
{
	EINT_InitTypeDef EINT_InitStructure;

	/* Configure the external interrupt */
	EINT_InitStructure.m_Fall = TRUE;
	EINT_InitStructure.m_Line = INT_EXTERNAL_INTERRUPT_4;
	EINT_InitStructure.m_Mask = TRUE;
	EINT_InitStructure.m_Rise = TRUE;
	EINT_InitStructure.m_Source = INT_EXTERNAL_SOURCE_PD;
	INT_External_Configuration(&EINT_InitStructure);
}


/*******************************************************************************
**                     			Main Functions 		             	     	  **
*******************************************************************************/
/**
 *  @brief :Main program
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
int main()
{
	/* Initialize the system clock is 72M*/
	SystemInit(72);
	/* Setup SysTick Timer as delay function, and input frequency is 72M */
	systick_delay_init(72);
	/* Initialize the LED IOs */
	BoardGpioInit();
	/* Configure PD4 in interrupt mode */
	BoardUserKeyInit();
	/* Enable and set EXTI4 Interrupt to the lowest priority */
	INT_Interrupt_Enable(INT_EINT4, TRUE);
	INT_Interrupt_Priority_Config(INT_EINT4, 7, 1);
	/* Configure interrupt priority group, default is 3VS1 */
	INT_Priority_Group_Config(INT_PRIORITY_GROUP_3VS1);
	/* Enable the global interrupt */
	INT_All_Enable(TRUE);

	while(1)
	{
		/* Turn ON/OFF the LED1 every 500ms */
		if (++TimingDelay >= 10)
		{
			TimingDelay = 0;
			GPIO_Toggle_Output_Data_Config(GPIOF_SFR, GPIO_PIN_MASK_11);
			/* The LED will enter the normal sleep mode
			 	 every 10 flashes and wait for wake-up */
			if (++SleepCount >= 10)
			{
				SleepCount = 0;
				asm("SLEEP");
			}
		}
		systick_delay_ms(50);
	}		
}

/**
 *  @brief : Reports the name of the source file and the source line number
 *           where the assert_param error has occurred.
 *  @param in :	file pointer to the source file name
 * 	@param in :	line assert_param error line source number
 *  @param out :None
 *  @retval :None
 */
void check_failed(uint8_t* File, uint32_t Line)
{
	/* User can add his own implementation to report the file name and line number,
		ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while(1)
	{
		;
	}
};
